package com.esd.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.esd.crm.entity.Address;
import com.esd.crm.entity.Orders;
import com.esd.crm.entity.PrintModel;
import com.esd.crm.entity.Product;
import com.esd.crm.service.AddressService;
import com.esd.crm.service.OrdersService;
import com.esd.crm.service.ProductService;

@Controller
@RequestMapping("/security")
public class PrintController {
	private static final Logger logger = LogManager.getLogger(PrintController.class);
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private ProductService productService;
	@Autowired
	private AddressService addressService;

	/**
	 * 跳转更新订单页
	 */
	@RequestMapping(value = "/print", method = RequestMethod.GET)
	public ModelAndView printGet(HttpSession session, HttpServletRequest request) {
		logger.info("打印");

		String orderNumber = request.getParameter("orderNumber");
		Map<String, Object> orderParam = new HashMap<String, Object>();
		orderParam.put("orderNumber", orderNumber);
		Orders order = ordersService.selectByOrderNumber(orderParam);

		order.setStatus("收单");
		ordersService.updateByPrimaryKeySelective(order);
		logger.info(order.toString());
		//Customer c = customerService.selectByPrimaryKey(order.getCustomerid());
		ModelAndView mv = new ModelAndView("print/print1");
		// 获取打印数据
		PrintModel pm = new PrintModel();
		pm.setFreightStationCard(order.getFreightstationcard());
		if (StringUtils.isNoneBlank(order.getAddress())) {
			Address address = addressService.selectByPrimaryKey(Integer.valueOf(order.getAddress()));
			pm.setName(address.getName());
			pm.setTelephone(address.getTelephone());
			if (StringUtils.isNotBlank(address.getDetailedAddress())) {
				pm.setAddress(address.getDetailedAddress());
			} else {
				pm.setAddress(address.getAddress());
			}
		}
		pm.setOrderNumber(orderNumber);
		pm.setTax(order.getTax() + "%");
		pm.setDate(order.getOrderCreateTime());
		pm.setPaymentstatus(order.getPaymentstatus());
		pm.setFreightStation(order.getFreightstation());
		pm.setTotal(order.getTotalStr());

		// =============
		//int totle = 1;
		List<Product> list = null;
		// 组合查询参数
		// int page = Integer.valueOf(request.getParameter("page"));
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(orderNumber)) {
			param.put("orderNumber", orderNumber);
		}
		param.put("page", (1 - 1) * 20);
		param.put("totle", 20);
		list = productService.selectByParam(param);

		pm.setProducts(list);

		mv.addObject("pm", pm);

		return mv;
	}

}
